package com.juyu.miliao.api.service;

import com.juyu.miliao.api.dao.mysql.BillDetailDao;
import com.juyu.miliao.api.dao.mysql.UserWalletDao;
import com.juyu.miliao.api.domain.BillDetail;
import com.juyu.miliao.api.domain.RedPacket;
import com.juyu.miliao.api.domain.UserWallet;
import com.juyu.miliao.common.exception.ConsumeRuntimeException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

@Service
@Slf4j
public class LuckyWheelService {

    @Resource
    private UserWalletDao userWalletDao;
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private BillDetailDao billDetailDao;

    private static final Map<Integer,Integer> map = new HashMap<>();
    static{
        map.put(1, 20);
        map.put(2, 100);
        map.put(3, 200);
    }


    public RedPacket computeLuckyWheel(int costType,long userId){

        // 获取要消费的金币数
        int inDeposit = map.get(costType);
        if (inDeposit==0)
            throw new ConsumeRuntimeException("无该计费点error:costType-"+costType+",inDeposit:"+inDeposit);
        // 获取帐号钱包
        UserWallet userWallet = userWalletDao.getUserWalletById(userId);
        // 判断钱包金币是否够用
        if (inDeposit > userWallet.getDeposit())
            throw new ConsumeRuntimeException("金币不足");
        // 获取奖池值
//        String strPool = stringRedisTemplate.opsForValue().get("redPacketPool");
        String strPool = "19";
        int pool = 0;
        if(strPool != null && !strPool.equals("") && !strPool.equals("null"))
            pool = Integer.valueOf(strPool).intValue();
        // 1.获取随机数范围
        int min = (int) (inDeposit * 0.2);
        int max = 0;
        if (inDeposit * 1.4 > inDeposit * 0.6 + pool)
            max = (int) (inDeposit * 0.6 + pool);
        else
            max = (int) (inDeposit * 1.4);
        // 2.获取随机值
        int randNum = getRandom(min, max);
        // 3.计算奖池数值
        pool = (int) (inDeposit * 0.6 - randNum + pool);
        stringRedisTemplate.opsForValue().set("redPacketPool", pool + "");

        // 保存消费记录-消费金币
        BillDetail billDetail = new BillDetail();
        billDetail.setRecordType(9);// 消费类型9幸运转盘消费金币 10幸运轮盘赚取金币
        billDetail.setInUserId(0);// 赚取钻石用户id
        billDetail.setOutUserId(userId);// 消费金币用户id
        billDetail.setDiamond(0);// 返回金币数
        billDetail.setSpend(inDeposit);// 消费金币数
        billDetailDao.insertBillDetail(billDetail);

        // 保存消费记录-赚取金币
        BillDetail billDetailInDiamond = new BillDetail();
        billDetailInDiamond.setRecordType(10);// 消费类型9幸运转盘消费金币 10幸运轮盘赚取金币
        billDetailInDiamond.setInUserId(0);// 赚取钻石用户id
        billDetailInDiamond.setOutUserId(userId);// 消费金币用户id
        billDetailInDiamond.setDiamond(randNum);// 返回金币数
        billDetailInDiamond.setSpend(0);// 消费金币数
        billDetailDao.insertBillDetail(billDetailInDiamond);

        // 用户钱包结算
        userWallet.setDeposit(randNum - inDeposit);
        userWallet.setUserId(userId);
        userWalletDao.reviseDeposit(userWallet);

        RedPacket redPacket = new RedPacket();
        redPacket.setUserId(userId);
        redPacket.setCostType(costType);
        redPacket.setInDeposit(inDeposit);
        redPacket.setOutDeposit(randNum);
        redPacket.setDifference(randNum - inDeposit);
        return redPacket;
    }

    private static int getRandom(int min, int max) {
        Random rand = new Random();
        return rand.nextInt(max - min + 1) + min;
    }
}
